<?php
/**
 * 显示题目列表
 *
 * @todo 考虑是否可以静态化
 * @author Judas <judasnow@gmail.com>
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
 */
session_start();
require_once('path.php');
require_once( SOJ_ROOT.'/include/function/lib.php');

require_once('Pager.php');

$tpl = new my_smarty();
$tpl->assign( 'active' , 'problem' ) ;

if( !empty($_SESSION['username']) ){
	$tpl->assign( 'username' , $_SESSION['username'] );
}

//处理由do_singin.php页面保存在会话中的错误信息
if( !empty($_SESSION['errors']) ){

	$tpl->assign( 'errors' , $_SESSION['errors']  );
	//清除之前的错误信息
	$_SESSION['errors'] = array();
}

try{
	$db = db_connect();
	$problems = problem_process::get_all( $db );
	if ( empty($problems) ){
		throw new Exception('题库为空');
	}
	//动态获得结果数组的键名
	$titles = array_keys( $problems[0] );
	$problem_num = count( $problems );

	//此配置应该可以由配置文件读出
	$params = array(
    		'itemData' => $problems,
    		'perPage' => 30,
    		'delta' => 8,             
    		'append' => true,
    		'clearIfVoid' => false,
    		'urlVar' => 'entrant',
    		'useSessions' => true,
    		'closeSession' => true,
    		'mode'  => 'Jumping',
	);
	
	$pager = & Pager::factory($params);
	$page_data = $pager->getPageData();
	$links = $pager->getLinks();

//	$selectBox = $pager->getPerPageSelectBox();

	$tpl->assign( 'titles' , $titles );
	$tpl->assign( 'problem_num' , $problem_num );
	$tpl->assign( 'problems' , $page_data );
	$tpl->assign( 'links' , $links['all'] );

	$tpl->display('problem_list.tpl');

}catch(Exception $e){

	exception_handle( $e );
}

